package com.killer.edu.message.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.killer.edu.common.result.ResultCode;
import com.killer.edu.common.util.ValidateUtils;
import com.killer.edu.course.api.CourseRemoteService;
import com.killer.edu.course.api.dto.CourseDTO;
import com.killer.edu.course.api.dto.LessonDTO;
import com.killer.edu.course.api.enums.CourseLessonStatus;
import com.killer.edu.message.entity.Message;
import com.killer.edu.message.mapper.MessageMapper;
import com.killer.edu.message.server.PushServer;
import com.killer.edu.message.service.IMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.killer.edu.order.api.UserCourseOrderRemoteService;
import com.killer.edu.order.api.dto.UserCourseOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 消息通知表 服务实现类
 * </p>
 *
 * @author killer
 * @since 2021-05-28
 */
@Service
@Slf4j
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {

    @Autowired
    private UserCourseOrderRemoteService userCourseOrderRemoteService;
    @Autowired
    private CourseRemoteService courseRemoteService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Integer> saveMessage(Integer courseId) {
        log.info("saveMessage - lessonId:{}",courseId);
        ValidateUtils.notNullParam(courseId);
        ValidateUtils.isTrue(courseId > 0, "课程id参数错误");
        List<UserCourseOrderDTO> userCourseOrderList = userCourseOrderRemoteService.getOrderListByCourseId(courseId);

        if(CollectionUtils.isEmpty(userCourseOrderList)) {
            log.warn("saveMessage - courseId:{} 没有查到支付成功的商品订单",courseId);
            return Lists.newArrayList();
        }
        CourseDTO courseDTO = courseRemoteService.getCourseById(courseId);
        ValidateUtils.notNull(courseDTO, ResultCode.ALERT_ERROR.getState(), "课程信息查询为空-courseId");

        List<Message> saveMessageList = new ArrayList<Message>();
        List<Integer> res = new ArrayList<Integer>();
        Message saveMessage = null;
        for(UserCourseOrderDTO userCourseOrder : userCourseOrderList) {
            saveMessage = new Message();
            saveMessage.setCourseId(userCourseOrder.getCourseId());
            saveMessage.setCourseName(courseDTO.getCourseName());
            saveMessage.setCreateTime(new Date());
            saveMessage.setUpdateTime(saveMessage.getCreateTime());
            saveMessage.setUserId(userCourseOrder.getUserId());
            saveMessageList.add(saveMessage);
            res.add(userCourseOrder.getUserId());
        }
        boolean saveRes = saveBatch(saveMessageList);
        ValidateUtils.isTrue(saveRes, "批量保存消息通知异常");
        return res;
    }

    /**
     * @author: ma wei long
     * @date:   2020年6月30日 下午4:57:36
     */
    @Override
    public void sendMessage(com.killer.edu.message.api.dto.Message message) {
        if(null == message) {
            return;
        }
        PushServer.pushServer.push(message);
    }
}
